#! /bin/sh
# test that the empty file means no pattern
# and an empty pattern means match all.
#
# Copyright (C) 2001, 2006, 2009-2020 Free Software Foundation, Inc.
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.

. "${srcdir=.}/init.sh"; path_prepend_ ../src

require_timeout_

failures=0

for locale in C en_US.UTF-8; do
    for options in '-E' '-F'; do

        # should return 0 found a match
        echo "" | LC_ALL=$locale timeout 10s grep $options -e ''
        if test $? -ne 0 ; then
          echo "Status: Wrong status code, test \#1 failed ($options $locale)"
          failures=1
        fi

        # should return 1 found no match
        echo abcd | LC_ALL=$locale timeout 10s grep $options -f /dev/null
        if test $? -ne 1 ; then
          echo "Status: Wrong status code, test \#2 failed ($options $locale)"
          failures=1
        fi

        # should return 0 found a match
        echo abcd \
            | LC_ALL=$locale timeout 10s grep $options -f /dev/null -e abcd
        if test $? -ne 0 ; then
          echo "Status: Wrong status code, test \#3 failed ($options $locale)"
          failures=1
        fi

        # should return 0 found a match
        echo "" | LC_ALL=$locale timeout 10s grep $options -e ''
        if test $? -ne 0 ; then
          echo "Status: Wrong status code, test \#4 failed ($options $locale)"
          failures=1
        fi

        # should return 0 found a match
        echo abcd | LC_ALL=$locale timeout 10s grep $options -e ''
        if test $? -ne 0 ; then
          echo "Status: Wrong status code, test \#5 failed ($options $locale)"
          failures=1
        fi
    done

    for options in '-E -w' '-E -x' '-E -w -x' '-F -w' '-F -x' '-F -w -x'; do

        # should return 0 found a match
        echo "" | LC_ALL=$locale timeout 10s grep $options -e ''
        if test $? -ne 0 ; then
          echo "Status: Wrong status code, test \#6 failed ($options $locale)"
          failures=1
        fi

        # should return 1 found no match
        echo abcd | LC_ALL=$locale timeout 10s grep $options -f /dev/null
        if test $? -ne 1 ; then
          echo "Status: Wrong status code, test \#7 failed ($options $locale)"
          failures=1
        fi

        # should return 1 found no match
        echo abcd | LC_ALL=$locale timeout 10s grep $options -f /dev/null -e ""
        if test $? -ne 1 ; then
          echo "Status: Wrong status code, test \#8 failed ($options $locale)"
          failures=1
        fi
    done
done

Exit $failures
